package org.example.salesystem.controller;

import org.example.salesystem.entity.ResultBody;
import org.example.salesystem.service.RegisterUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/RegisterUser")
@CrossOrigin(origins = "*")
public class RegisterUserController {
    @Autowired
    private RegisterUserService registerUserService;
    @RequestMapping("/registerUser")
    public ResultBody registerUser(@RequestBody Map<String,Object> map) {
        if(map.get("username")==null ||map.get("password") ==null|| map.get("confirmPassword") ==null||map.get("phone")==null||map.get("email")==null) {
            return new ResultBody(404, "缺少参数", false);
        }
        String username = map.get("username").toString();
        String password1 = map.get("password").toString();
        String password2 = map.get("confirmPassword").toString();
        String phonenumber = map.get("phone").toString();
        String email = map.get("email").toString();
        if(!map.get("password").equals(map.get("confirmPassword"))) {
            return new ResultBody(400,"重复密码错误",false);
        }
        if(registerUserService.checkPhoneNumber(phonenumber)==1)
        {
            return new ResultBody(400,"电话重复",false);
        }
        if(registerUserService.checkEmail(email)==1)
        {
            return new ResultBody(400,"邮箱重复",false);
        }
        if(registerUserService.checkUserName(username)==1)
        {
            return new ResultBody(400,"用户名称重复",false);
        }
        if(registerUserService. RegisterUser(username, password1, phonenumber,email)==1)
        {
            return new ResultBody(200,"用户注册成功",true);
        }
        else
            return new ResultBody(400,"用户注册失败",false);
    }
    //参数判断
    //重复密码判断
    //电话号码，密码，邮箱是否重复
    //传值
}
